<div class="row">
    <div class="col-lg-12">
        <h3 class="page-header">Thêm Hệ số điểm sản phẩm</h3>
    </div>
    <!-- /.col-lg-12 -->
</div>
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <div class="row">
                    <div class="col-lg-6">
                        <form role="form" method="POST" name="AddAreaAgentProduct" onsubmit="return checkinput();">
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Khu vực</label>
                                    </div>
                                    <div class="col-lg-6">
                                        <?php echo $this->areaHTML; ?>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Tỉnh thành</label>
                                    </div>
                                    <div class="col-lg-6" id="province">
                                        <?php echo $this->provinceHTML; ?>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Quận huyện</label>
                                    </div>
                                    <div class="col-lg-6" id="district">
                                        <?php echo $this->districtHTML; ?>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Đại lý/Cửa hàng</label>                                        
                                    </div>
                                    <div class="col-lg-6" id="agent">
                                        <?php echo $this->agentHTML; ?>
                                    </div>
                                </div>
                            </div>  
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Nhóm Sản phẩm </label>                                        
                                    </div>
                                    <div class="col-lg-6">
                                        <?php echo $this->productcategoryHTML; ?>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Sản phẩm </label>                                        
                                    </div>
                                    <div class="col-lg-6" id="product">
                                        <?php echo $this->productHTML; ?>
                                    </div>
                                </div>
                            </div> 
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Hệ số điểm</label>
                                    </div>
                                    <div class="col-lg-6">
                                        <input class="form-control" name="factor" id="factor" value="<?php echo $this->item->getFactor(); ?>">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Từ ngày</label>
                                    </div>
                                    <div class="col-lg-6">
                                        <input class="form-control" type="datetime" name="from_date" id="from_date" value="" >
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Đến ngày</label>
                                    </div>
                                    <div class="col-lg-6">
                                        <input class="form-control" type="datetime" name="to_date" id="to_date" value="">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Sư kiện</label>
                                    </div>
                                    <div class="col-lg-6">
                                        <input type="checkbox" name="event" id="event" value="1" 
                                               <?php echo ($this->item->getEvent() == 1 ? "checked=\"checked\"" : "") ?> >
                                    </div>
                                </div>
                            </div>
                              <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <label>Thứ tự</label>
                                    </div>
                                    <div class="col-lg-6">
                                        <input class="form-control" type="input" name="order" id="order" value="<?php echo $this->item->getOrder() ?>" >
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary">Lưu</button>
                                <button type="reset" class="btn btn-default">Đặt lại</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>   
</div>
<script type="text/javascript">    
    document.forms['AddAreaAgentProduct'].name.focus();
    function set_focus() {
        document.forms['AddAreaAgentProduct'].name.focus();
    }
    function checkinput() {
        var frm = window.document.AddAreaAgentProduct;
      
        if (frm.product_id.value == "0" && frm.product_category_id.value == "0") {
            bootbox.alert("Vui lòng chọn sản phẩm hoặc nhóm sản phẩm!");
            return false;
        }
        
        if (frm.factor.value == "" || isNaN(frm.factor.value)==true) {
            bootbox.alert("Vui lòng nhập hệ số điểm sản phẩm!");
            return false;
        }
        if ($("#from_date").val() == "") {
            bootbox.alert("Vui lòng chọn ngày bắt đầu!");
            return false;
        }
        if ($("#from_date").val()== "") {
            bootbox.alert("Vui lòng chọn ngày kết thúc!");
            return false;
        }
        var f = $("#from_date").datepicker("getDate");
        var t = $("#to_date").datepicker("getDate");        
        if(f > t)
        {
            bootbox.alert("Ngày bắt đầu không thể lớn hơn ngày kết thúc");
            return false;
        }
        return true;
    }
</script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#factor").keydown(function (e) {
            // Allow: backspace, delete, tab, escape, enter and .
            if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
                 // Allow: Ctrl+A
                (e.keyCode == 65 && e.ctrlKey === true) || 
                 // Allow: home, end, left, right
                (e.keyCode >= 35 && e.keyCode <= 39)) {
                     // let it happen, don't do anything
                     return;
            }
            // Ensure that it is a number and stop the keypress
            if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                e.preventDefault();
            }
        });
    });
    $('#agent_id').multiselect({
            includeSelectAllOption:true,
            enableFiltering: true,
            maxHeight: '300'
        });
    $('#district_id').multiselect({
            includeSelectAllOption:true,
            enableFiltering: true,
            maxHeight: '300'
        });
    $('#product_id').multiselect({
            includeSelectAllOption:true,
            enableFiltering: true,
            maxHeight: '300'
        });
     var nowTemp = new Date();
    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
     var checkin = $('#from_date').datepicker({  
         dateFormat:'dd-mm-yy',
         changeMonth: true,
         changeYear: true,
  onRender: function(date) {
    return date.valueOf() < now.valueOf() ? 'disabled' : '';
  }
    }).on('changeDate', function(ev) {
    if (ev.date.valueOf() > checkout.date.valueOf()) {
      var newDate = new Date(ev.date)
      newDate.setDate(newDate.getDate() + 1);
      checkout.setValue(newDate);
  }
  checkin.hide();
  $('#to_date')[0].focus();
}).data('datepicker');
var checkout = $('#to_date').datepicker({
 dateFormat:'dd-mm-yy',
         changeMonth: true,
         changeYear: true,
  onRender: function(date) {
    return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
  }
}).on('changeDate', function(ev) {
  checkout.hide();
}).data('datepicker');
</script>
<script>
    $('#product_category_id').multiselect({
            enableFiltering: true,
            maxHeight: '300',
            onChange: function(option, checked) {
                  $.post("<?php echo $this->baseUrl() ?>/admin/product/getproduct/", 
            {
                product_cat_id: $('#product_category_id').val()
            },function (datas) {
                var items = eval(datas);
                var jItem = items[0];
                         $('#product').html(jItem.html);
                         $('#product_id').multiselect();
                    });
                if (checked) {
                    orderCount++;
                    $(option).data('order', orderCount);
                }
                else {
                    $(option).data('order', '');
                }
            },
        });
    $('#province_id').multiselect({
            enableFiltering: true,
            onChange: function(option, checked) {
                  $.post("<?php echo $this->baseUrl() ?>/admin/district/getdistrict/", 
            {
                province_id: $('#province_id').val()
            },function (datas) {
                var items = eval(datas);
                var jItem = items[0];
                         $('#district').html(jItem.html);
                         $('#district_id').multiselect();
                    });
                if (checked) {
                    orderCount++;
                    $(option).data('order', orderCount);
                }
                else {
                    $(option).data('order', '');
                }
            },
        });
        $('#district_id').multiselect({
            enableFiltering: true,
            onChange: function(option, checked) {
                if (checked) {
                    orderCount++;
                    $(option).data('order', orderCount);
                }
                else {
                    $(option).data('order', '');
                }
            },
        });
        $('#agent_id').multiselect({
            enableFiltering: true,
            onChange: function(option, checked) {
                if (checked) {
                    orderCount++;
                    $(option).data('order', orderCount);
                }
                else {
                    $(option).data('order', '');
                }
            },
        });
        $('#product_id').multiselect({
            enableFiltering: true,
            onChange: function(option, checked) {
                if (checked) {
                    orderCount++;
                    $(option).data('order', orderCount);
                }
                else {
                    $(option).data('order', '');
                }
            },
        });
        $('#area_id').multiselect({
            enableFiltering: true,
            onChange: function(option, checked) {
                 $.post("<?php echo $this->baseUrl() ?>/admin/province/getprovince/", 
            {
                area_id: $('#area_id').val()
            },function (datas) {
                var items = eval(datas);
                var jItem = items[0];
                         $('#province').html(jItem.html);
                         $('#province_id').multiselect({
                            enableFiltering: true,
                            onChange:function(option,checked){
                                $.post("<?php echo $this->baseUrl()?>/admin/district/getdistrict/",{
                                    province_id:$('#province_id').val()
                                },function(datas){
                                    var items=eval(datas);
                                    var jItem=items[0];
                                    $('#district').html(jItem.html);
                                    $('#district_id').multiselect({
                                        enableFiltering: true,
                                        onChange:function(option,checked){
                                           $.post("<?php echo $this->baseUrl()?>/admin/agent/getagentdistrict",{
                                               district_id:$('#district_id').val()
                                           },function(datas){
                                               var items=eval(datas);
                                               var jItem=items[0];
                                               $('#agent').html(jItem.html);
                                               $('#agent_id').multiselect();
                                           }) 
                                        },
                                    });
                                });
                            },
                         });
                    });
                    
                if (checked) {
                    orderCount++;
                    $(option).data('order', orderCount);
                    
                }
                else {
                    $(option).data('order', '');
                }
            },
        });
</script>